package com.woniuxy.yoga.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.yoga.pojo.Cadetail;
import com.woniuxy.yoga.pojo.Coach;
import com.woniuxy.yoga.pojo.Coursecoach;
import com.woniuxy.yoga.service.CadetailService;
import com.woniuxy.yoga.service.CoachService;
import com.woniuxy.yoga.service.CoursecoachService;
import com.woniuxy.yoga.service.OrdersService;
import com.woniuxy.yoga.utils.R;
import io.swagger.annotations.ApiOperation;
import org.omg.CORBA.Current;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author axq
 * @since 2020-10-30
 */
@RestController
@RequestMapping("/yoga/cadetail")
public class CadetailController {

    @Autowired
    private CadetailService cadetailService;
    @Autowired
    private CoachService coachService;

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private CoursecoachService coursecoachService;

    @CrossOrigin
    @ApiOperation("查询(登录本人)(身份教练)评价信息")
    @GetMapping(value = "coachDetails/{accountid}")
    public R coachDetails(@PathVariable Integer accountid){
        //用当前登录的账户id,到教练表中查询出教练id
        Integer coachid = coachService.getOne(new QueryWrapper<Coach>().eq("accountid", accountid)).getCoachid();
       //通过教练id到评价详情表中查询出该教练的全部评价
        QueryWrapper query=new QueryWrapper();
        query.eq("coachid",coachid);
        List list = cadetailService.list(query);
        System.out.println(list.size());
        if (list!=null){
            return R.ok().data("details",list).message("查询成功");
        }
        return R.error().message("查询失败");

    }

    //添加一条评价信息
    @CrossOrigin
    @ApiOperation("插入一条用户对教练的评价信息(accountid：登录本人)")
    @PostMapping(value = "saveOne/{ordered}")
    public R saveOne(@PathVariable("ordered")Integer ordered, @RequestBody Cadetail form){
        System.out.println(ordered);
        //通过订单id得到课程id
        Integer courseid = ordersService.getCourseByOrderId(ordered);
        //通过课程id得到教练id
        Integer coachid = coursecoachService.getOne(new QueryWrapper<Coursecoach>().eq("courseid", courseid)).getCoachid();

        //将评价对象-教练封装到对象中
        form.setCoachid(coachid);
        form.setCadtime(new Date());

        boolean b = cadetailService.save(form);

        if (b){
            return R.ok().message("发表评价成功");
        }else{
            return R.error().message("评价失败，稍后重试");
        }
    }
}

